<nav>
	<ul>
		<li><a href="?page=structure&amp;structure=<?php echo U1_Building::INDEX;?>" title="affiche les bâtiments de la planète">bâtiment</a></li>
		<li><a href="?page=structure&amp;structure=<?php echo U1_Research::INDEX;?>" title="affiche les recherches de la planète">recherches</a></li>
		<li><a href="?page=structure&amp;structure=<?php echo U1_Army::INDEX;?>" title="affiche les armes de la planète">armes</a></li>
	</ul>
</nav>
<?php
$messagePlanets = new stdClass;

$strucKey = array(U1_Building::INDEX, U1_Research::INDEX, U1_Army::INDEX);

//sélection de la structure à afficher
if (isset($_GET['structure']) && in_array($_GET['structure'], $strucKey, true))
	$strucKey = array($_GET['structure']);


//récupère les batiments et les recherches terminé
$completedLists = $player->getCompletedLists();
foreach ($planets->getIds() as $id_p)
{
	//message
	$messagePlanets->$id_p = new stdClass;
	foreach($strucKey as $indexStruct)
		$messagePlanets->$id_p->$indexStruct = array();
	$messagePlanets->$id_p->{U1_Resource::INDEX} = array();


	foreach ($strucKey as $indexStruct)
	{
		$structure = $planets->getInstance($indexStruct, $id_p);
		$displayStruct = U1_Display_Structure::getdisplay($indexStruct);

		foreach (isset($completedLists[$id_p][$indexStruct])
			? array_merge($completedLists[$id_p][$indexStruct], $structure->getCompletedLists())
			: $structure->getCompletedLists()
			as $l)
		{
			array_push(
				$messagePlanets->$id_p->$indexStruct,
				$indexStruct.'['.$displayStruct->getName($structure->get($l->getStructureId())->getGroup()).'] (id '.$l->getStructureId().' list '.$l->getId().') terminer en '.$planets->get($id_p)->targetGlue()
			);
		}
	}
}

//construction d'une structure
if (isset ($_GET['query'], $_GET['planet'], $_GET['structure'], $_GET['id']))
{
	if (($_GET['query'] === 'build' || $_GET['query'] === 'down')
	 && $planets->exists($_GET['planet'])
	 && false !== array_search($_GET['structure'], $strucKey))
	{
		$query = $_GET['query'];
		$indexStruct = $_GET['structure'];
		$id_p = (int)$_GET['planet'];
		$id_s = (int)$_GET['id'];
		$structure = $planets->getInstance($indexStruct, $id_p);

		$name = ('build' === $query) ? (($e = $structure->get($id_s)) ? $e->getGroup() : -1) : (($l = $structure->getList($id_s)) ? $l->getStructureId() : -1);

		$message = 'Planete '.$id_p.' ('.$planets->get($id_p)->targetGlue().') : '.$query.'['.$indexStruct.'] : '.U1_Display_Structure::getdisplay($indexStruct)->getName($name);
		try{
			$structure->$query($id_s, $_SERVER['REQUEST_TIME'], 1);
		}
		catch (SW_Exception $e)
		{
			$message .= ' => '.$e->getMessage();
		}

		array_push($messagePlanets->$id_p->$indexStruct, $message);
	}
}

echo '<pre>';
var_dump($_GET);
echo '</pre>';


$id_p = $planets->getCurrentId();
$planet = $planets->getCurrentPlanet();
?>
<section id="info-joueur">
	<ul>
		<li>Joueur : <?php echo $player->getName().' ('.$player->getId().')';?></li>
		<li>Race : <?php echo SW_Race::getName($player->getRace()).' ('.$player->getRace().')';?></li>
		<li>Id de la planète sélectionner : <?php echo $id_p;?>
			<ul>
				<li>nom : <?php echo $planet->getName();?></li>
				<li>coordonnées : <?php echo $planet->targetGlue();?></li>
			</ul>
		</li>
	</ul>
</section>

<section class="resource">
<h1>Ressource</h1>
<ul>
<?php
$resources = $planets->getInstance(U1_Resource::INDEX, $id_p);
foreach ($resources/*->gets()*/ as $group => $resource)
{
	echo '<li>'.U1_Display_Resource::getName($group).'<ul><li>quantité : '.$resource.'</li><li>par heure : '.$resource->getByHour().'</li></ul></li>';
}
?>
</ul>
<section class="dependence">
<h2>Dépendance des ressources</h2>
<ul>
<?php
foreach ($resources->getDependency() as $id_asset => $group_ressource)
{
	echo '<li>'.U1_Display_Resource::getName($id_asset);
	foreach ($group_ressource as $id_type => $ids_group)
	{
		echo '<ul><li>type '.$id_type.'<ul>';
		foreach($ids_group as $group)
			echo '<li>'.U1_Display_Building::getName($group).'</li>';
		echo '</ul></li></ul>';
	}
	echo '</li>';
}
?>
</section>
</ul>
</section>
<?php
$displayResource = U1_Display_Structure::getdisplay(U1_Resource::INDEX);
//boucle sur chaque structure
foreach ($strucKey as $indexStruct)
{
	$structure = $planets->getInstance($indexStruct, $id_p);
	$displayStruct = U1_Display_Structure::getdisplay($indexStruct);

	echo '<section class="structure '.$indexStruct.'"><h1>'.$indexStruct.'</h1>';

	if (!empty($messagePlanets->$id_p->$indexStruct))
	{
		echo '<section class="information"><h2>Message</h2>';
		foreach ($messagePlanets->$id_p->$indexStruct as $message)
			echo '<p class="message">'.$message.'</p>';
		echo '</section>';
	}
	echo '<section class="structure-list"><h2>Liste de construction</h2>';

	foreach($structure->getLists() as $l)
	{
		echo '<pre>';
		var_dump($l, $displayStruct->getName($structure->get($l->getStructureId())->getGroup()));
		echo '</pre>';
		echo '<p>'.($l->getTimeEnd() - $_SERVER['REQUEST_TIME']).'s<br /><a href="?planet='.$structure->getIdPlanet().'&amp;query=down&amp;structure='.$indexStruct.'&amp;id='.$l->getId().'">arrêter</a></p>';
	}

	echo '</section><section class="structure-available"><h2>Disponible</h2>';
	$ul = $display = '';
	foreach ($structure->getGroupsUnblock() as $group => $elements)
	{
		foreach ($elements as $element)
		{
			$lvl = $element->getLevel();
			$id_e = $element->getId();

			$tmp = $displayStruct->getName($group).' [lvl⋅'.$lvl.']';
			$ul .= '<li><a href="#structure-'.$id_e.'">'.$tmp.'</a></li>';
			$display .= '<section class="structure element"><h3 id="structure-'.$id_e.'">'.$tmp.'</h3><div class="action-list"><div class="action"><p class="action"><a href="?planet='.$id_p.'&amp;query=build&amp;structure='.$indexStruct.'&amp;id='.$id_e.'">Construire le niveau '.($lvl+1).'</a></p><ul><li>Prix<ul>';
			foreach ($element->getPrice() as $id => $cout)
				$display .= '<li>'.$displayResource->getName($id).' : '.$cout.'</li>';
			$display .= '</ul></li><li>Durée : '.$element->getDuration().'s</li>';
			if ($indexStruct === U1_Research::INDEX)
				$display .= '<li>évolution : '.$element->getGainForTheDuration().'p (+'.($element->getPercentByTheEndOfTheDuration() - $element->getRealLevel()).'%)</li>';
			$display .= '</ul></div></div></section>';
		}
	}
	echo '<section class="structure-available-list"><nav><ul>'.$ul.'</ul></nav></section>'.$display.'</section>';
}
?>